package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import pers.cj.pdos.entity.PayOrder;
import pers.cj.pdos.entity.PayWait;
import pers.cj.pdos.exception.CustomException;
import pers.cj.pdos.mapper.PayOrderMapper;
import pers.cj.pdos.mapper.PayWaitMapper;
import pers.cj.pdos.mapper.SettAccountMapper;
import pers.cj.pdos.service.IPayOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.utils.UuidUtils;
import pers.cj.pdos.vo.InGoodsVo;
import pers.cj.pdos.vo.PayOrderVo;
import pers.cj.pdos.vo.PayWaitVo;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class PayOrderServiceImpl extends ServiceImpl<PayOrderMapper, PayOrder> implements IPayOrderService {
    @Autowired
    PayOrderMapper payOrderMapper;
    @Autowired
    PayWaitMapper payWaitMapper;
    @Autowired
    SettAccountMapper settAccountMapper;

    @Override
    public boolean checkNo(String no, String companyId) {
        return payOrderMapper.selectCount(new QueryWrapper<PayOrder>().eq("company_id",companyId).eq("no",no)) > 0;
    }

    @Override
    public void insert(PayOrder payOrder) {
        payOrderMapper.insert(payOrder);
        //同步处理应付欠款
        processPayWait(payOrder);

        //付款 余额-
        settAccountMapper.addBalance(payOrder.getThisPay().negate(), payOrder.getSettAccountId());
    }

    @Override
    public boolean checkClose(PayOrder payOrder) {
        return payOrderMapper.selectCount(new QueryWrapper<PayOrder>().eq("id",payOrder.getId()).eq("close",true)) > 0;
    }

    @Override
    public void close(PayOrder payOrder) {
        PayOrder db = payOrderMapper.selectById(payOrder.getId());
        if (db == null) {
            throw new CustomException("参数id不正确，未找到原付款单");
        }


        //付款作废 余额+
        settAccountMapper.addBalance(db.getThisPay(), db.getSettAccountId());

        PayWaitVo payWaitVo = payWaitMapper.getVoByProvider(db.getProviderId());
        if (payWaitVo != null) {
            PayWait payWait = new PayWait();
            payWait.setId(payWaitVo.getId());
            payWait.setWaitPay(payWaitVo.getWaitPay().add(db.getTotal()));
            payWait.setHasPay(payWaitVo.getHasPay().subtract(db.getThisPay()));
            payWait.setDiscount(payWaitVo.getDiscount().subtract(db.getDiscount()));
            payWaitMapper.updateById(payWait);
        }

        PayOrder temp = new PayOrder();
        temp.setId(payOrder.getId());
        temp.setClose(true);
        payOrderMapper.updateById(temp);
    }

    @Override
    public PayOrderVo getVoById(String id) {
        return payOrderMapper.getVoById(id);
    }

    private void processPayWait(PayOrder payOrder) {
        PayWaitVo payWaitVo = payWaitMapper.getVoByProvider(payOrder.getProviderId());
        if (payWaitVo != null) {
            PayWait payWait = new PayWait();
            payWait.setId(payWaitVo.getId());
            payWait.setWaitPay(payWaitVo.getWaitPay().subtract(payOrder.getTotal()));
            payWait.setHasPay(payWaitVo.getHasPay().add(payOrder.getThisPay()));
            payWait.setDiscount(payWaitVo.getDiscount().add(payOrder.getDiscount()));
            if (payWait.getWaitPay().compareTo(new BigDecimal("0")) == 0) {
                payWait.setWaitPay(new BigDecimal("0"));
            }
            payWaitMapper.updateById(payWait);
        } else {
            PayWait payWait = new PayWait();
            payWait.setId(UuidUtils.getUUID());
            payWait.setProviderId(payOrder.getProviderId());
            payWait.setTotal(new BigDecimal("0"));
            payWait.setHasPay(payOrder.getThisPay());
            payWait.setDiscount(payOrder.getDiscount());
            payWait.setWaitPay(new BigDecimal("-" + payOrder.getTotal().toString()));
            payWait.setCompanyId(payOrder.getCompanyId());
            payWaitMapper.insert(payWait);
        }
    }
}
